(function ($) {
  $.fn.extend({
    bindAkeTree: function (options, vm) {
      var data = {
        "data": {
          "children": [{
            "children": [{
              "children": [{
                "children": [{
                  "children": [{
                    "childrenNum": 0,
                    "groupId": 26,
                    "isArea": false,
                    "isParent": false,
                    "name": "简平路停车点",
                    "open": false,
                    "value": "43"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "ake测试停车场",
                    "open": false,
                    "value": "3430"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "测试停车场",
                    "open": false,
                    "value": "3429"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "中庭停车点",
                    "open": false,
                    "value": "77"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "南海妇幼保健院",
                    "open": false,
                    "value": "3424"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "瀚天科技城",
                    "open": false,
                    "value": "2"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "佳盛国际广场停车场",
                    "open": false,
                    "value": "88"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "南海保利水城",
                    "open": false,
                    "value": "61"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "瀚天科技城地下停车场",
                    "open": false,
                    "value": "79"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "南海中医院",
                    "open": false,
                    "value": "85"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "江南名居锦苑停车场",
                    "open": false,
                    "value": "3419"
                  }, {
                    "childrenNum": 0,
                    "groupId": 26,
                    "isArea": false,
                    "isParent": false,
                    "name": "顺德大良新一城",
                    "open": false,
                    "value": "51"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "江南名居熙苑停车场",
                    "open": false,
                    "value": "3420"
                  }, {
                    "childrenNum": 0,
                    "groupId": 26,
                    "isArea": false,
                    "isParent": false,
                    "name": "南海万达广场",
                    "open": false,
                    "value": "59"
                  }],
                  "childrenNum": 14,
                  "isArea": true,
                  "isParent": true,
                  "name": "桂城(14)",
                  "open": false,
                  "value": "440605011"
                }, {
                  "children": [{
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "南海狮山和信广场",
                    "open": false,
                    "value": "60"
                  }],
                  "childrenNum": 1,
                  "isArea": true,
                  "isParent": true,
                  "name": "狮山镇(1)",
                  "open": false,
                  "value": "440605124"
                }, {
                  "childrenNum": 0,
                  "groupId": 26,
                  "isArea": false,
                  "isParent": false,
                  "name": "佛山南海万科广场",
                  "open": false,
                  "value": "3406"
                }],
                "childrenNum": 16,
                "isArea": true,
                "isParent": true,
                "name": "南海区(16)",
                "open": false,
                "value": "440605000"
              }, {
                "children": [{
                  "children": [{
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "乐从新世纪停车场",
                    "open": false,
                    "value": "3425"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "乐从新时代停车场",
                    "open": false,
                    "value": "3423"
                  }, {
                    "childrenNum": 0,
                    "groupId": 6,
                    "isArea": false,
                    "isParent": false,
                    "name": "乐从名苑停车场",
                    "open": false,
                    "value": "3422"
                  }],
                  "childrenNum": 3,
                  "isArea": true,
                  "isParent": true,
                  "name": "乐从镇(3)",
                  "open": false,
                  "value": "440606103"
                }, {
                  "childrenNum": 0,
                  "groupId": 6,
                  "isArea": false,
                  "isParent": false,
                  "name": "顺德凤城食都",
                  "open": false,
                  "value": "3390"
                }, {
                  "childrenNum": 0,
                  "groupId": 6,
                  "isArea": false,
                  "isParent": false,
                  "name": "乐从新丰停车场",
                  "open": false,
                  "value": "3417"
                }],
                "childrenNum": 5,
                "isArea": true,
                "isParent": true,
                "name": "顺德区(5)",
                "open": false,
                "value": "440606000"
              }],
              "childrenNum": 21,
              "isArea": true,
              "isParent": true,
              "name": "佛山市(21)",
              "open": false,
              "value": "440600000"
            }],
            "childrenNum": 21,
            "isArea": true,
            "isParent": true,
            "name": "广东(21)",
            "open": false,
            "value": "440000000"
          }],
          "childrenNum": 21,
          "isArea": true,
          "isParent": true,
          "name": "全国(21)",
          "open": false,
          "value": "0"
        },
        "message": "OK",
        "resultCode": 0,
        "status": 1
      };
      var defaults = {
        checkType: 'radio',
        id: 'tableTree',
        zNodes: data.data,
        type: '',
        status: "",
      };
      var options = $.extend(defaults, options);
      var curPageCache = options.curPageCache  
      var parkIds = curPageCache.parkIds ? curPageCache.parkIds : ''
      var parkSysType = (curPageCache.parkSysType===0 || curPageCache.parkSysType) ? curPageCache.parkSysType : -1
      return this.each(function () {
        var This = $(this);
        var nodeList = [],
          checkParams = {
            enable: true,
            chkStyle: "radio",
            chkboxType: {
              "Y": "",
              "N": ""
            },
          };
        if (vm.isCheckbox) {
          checkParams = {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {
              "Y": "",
              "N": ""
            }
          }
        } else {
          checkParams = {
            enable: true,
            chkStyle: "radio",
            radioType: "all",
          };
        };
        var setting = {
          data: {
            simpleData: {
              enable: true
            },
            key:{
              title:"title"
            }
          },
          check: checkParams,
          view: {
            showIcon: false,
            showLine: false,
            // fontCss: getFontCss,
            showTitle: true,
            nameIsHTML: true
          },
          callback: {
            beforeClick: judgeHasCheckedNode,
            beforeCheck: judgeHasCheckedNode,
            onClick: onClickNode,
            onCheck: onCheckNode,
          }
        };
        let ztreeData = options.zNodes.children
        let allChildren = []
        for(let i = 0; i<ztreeData.length; i++){
          // 停车场所属省份名
          let objName = adjustProvinceNameOfParkingLot(ztreeData[i].name)
          let objArr = []
          if(ztreeData[i].childrenNum && ztreeData[i].children){
            objArr = getZtreeChildNodes(ztreeData[i].children,'')
            objArr.sort((a,b)=>{
              return a.orderValue-b.orderValue
            })
            for(let j = 0; j<objArr.length; j++){
              objArr[j].name = "<font style='color:#888888;padding:2px 10px;background: #F4F4F4;border-radius: 2px;'>" + objName + '</font> ' + objArr[j].name
              objArr[j].title = objName + ' > ' + objArr[j].title
              if(parkIds.indexOf(objArr[j].value+'')>-1){
                objArr[j].checked = true
              }
              allChildren.push(objArr[j])
            }
          }
        }

        if(!parkIds){ //没有选过任何停车场
          allChildren[0].checked = true
          let treeNodeObj = allChildren[0]
          parkSysType = treeNodeObj.parkSysType
          setSelectedNode(treeNodeObj.name, treeNodeObj.value, treeNodeObj.parkSysType)
        }

        options.zNodes.chkDisabled = false
        options.zNodes.nocheck = false
        options.zNodes.children = allChildren
        var treeObj = $.fn.zTree.init($("#" + options.id), setting, options.zNodes);
        treeObj.expandAll(true);
        let allNodesObj = treeObj.getNodes();
        if(parkSysType!=-1){
          let allNodes = allNodesObj[0].children; //获取全部子节点
          for(let i = 0; i<allNodes.length; i++){
            if(allNodes[i].parkSysType!=parkSysType){
              treeObj.setChkDisabled(allNodes[i], true);
            }
          }
        }

        function getFontCss(treeId, treeNode) {
          return (!!treeNode.highlight) ? {
            color: "#558cd1",
          } : {
            color: '',
          };
        };

        function replaceWord(str) {
          var reg = /[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g;
          str = str.replace(reg, "");
          return str.replace(/\d+/g, '');
        };

        function judgeHasCheckedNode(treeId, treeNode, clickFlag){
         return vm.JudgeHasCheckedNode(treeId, treeNode, clickFlag)
        }

        function setSelectedNode(name, parkId, parkSysType){
          vm.setSelectedNode(name, parkId, parkSysType)
        }

        function onClickNode(event, treeId, treeNode) {
          vm.OnClickNode(event, treeId, treeNode);
        }

        function onCheckNode(event, treeId, treeNode) {
          vm.OnCheckNode(event, treeId, treeNode);
        }
        // 获取ztree的全部子节点
        function getZtreeChildNodes(nodes,nameStr){
          if(!nodes || !nodes.length){
            return [];
          }else{
            let arr = []
            for(let i = 0; i<nodes.length; i++){
              if(nodes[i].childrenNum){
                let name = nodes[i].name.replace(/\(.*?\)/g, '')
                name = (nameStr ? nameStr+' > ' : '') + name
                let tmpArr = getZtreeChildNodes(nodes[i].children,name)
                arr = tmpArr.concat(arr)
              }else{
                nodes[i].title = nameStr
                arr.push(nodes[i])
              }
            }
            return arr;
          }
        }
        // 调整停车场所属省份名
        function adjustProvinceNameOfParkingLot(provinceName){
          if(!provinceName){
            return provinceName
          }
          let idx = 0
          let rsStr = ''
          provinceName = provinceName.replace(/\(.*?\)/g, '')
          if(provinceName.indexOf('省')>-1){
            idx = provinceName.indexOf('省')
          }else if(provinceName.indexOf('自治区')>-1){
            idx = provinceName.indexOf('自治区')
          }else if(provinceName.indexOf('市')>-1){
            idx = provinceName.indexOf('市')  
          }else if(provinceName.length > 6){
            idx = 1
          }
          if(idx!==0){
            rsStr = provinceName.substring(0,idx)
            return rsStr
          }else{
            return provinceName
          }
        }
      });

    }
  })

})(jQuery);

function resetChart() {
  if (echarts != undefined) {
    echarts.getInstanceByDom($("#ecLine")[0]).resize();
    echarts.getInstanceByDom($("#ecHeat")[0]).resize();
  }
}
